﻿Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Runtime.InteropServices

Public Class FormMain
    Implements Widgets.ITaskTimerView
    Implements Widgets.IWorkItems

    Private WithEvents wgt_TaskTimer As New Widgets.TaskTimer(Of FormMain)(Me)
    Private WithEvents wgt_WorkItems As New Widgets.WorkItems(Of FormMain)(Me)

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        MyBase.SetBounds((Screen.GetWorkingArea(Me).Width - MyBase.Width), (Screen.GetWorkingArea(Me).Height - MyBase.Height), MyBase.Width, MyBase.Height)

    End Sub

    Private m_SelectedWI As WorkItem = Nothing

    Public Property SelectedWI() As WorkItem
        Get
            Return m_SelectedWI
        End Get
        Set(ByVal value As WorkItem)
            m_SelectedWI = value
        End Set
    End Property

#Region " FormMain Handlers "

    Private Sub FormMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        wgt_WorkItems.Start()
    End Sub

    Private Sub ux_RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_RefreshToolStripMenuItem.Click
        wgt_WorkItems.Start()
    End Sub

    Private Sub ux_StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_StartButton.Click
        ux_StopButton.Enabled = False
        wgt_TaskTimer.Start(New TimeSpan(0))
    End Sub

#End Region

#Region " TaskTimer Widget "

    Public Sub OnTaskTimer_Stop(ByVal Elapsed As System.TimeSpan) Implements Widgets.ITaskTimerView.OnTaskTimer_Stop

    End Sub

    Private Sub wgt_TaskTimer_Update(ByVal Elapsed As System.TimeSpan, ByVal IsIdle As Boolean) Handles wgt_TaskTimer.Update
        Dim Result As String = String.Format("{0} seconds", Elapsed)
        If IsIdle Then Result = String.Format("{0} (Idle)", Result)
        Me.ux_WorkingForValue.Text = Result
    End Sub

#End Region

#Region " Work Items Widget "

    Private Sub ux_StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_StopButton.Click

    End Sub

    Public Sub OnWorkItems_ServerCreated(ByVal Server As System.Windows.Forms.ToolStripMenuItem) Implements Widgets.IWorkItems.OnWorkItems_ServerCreated

    End Sub

    Private Sub wgt_WorkItems_Finished() Handles wgt_WorkItems.Finished

    End Sub

    Private Sub wgt_WorkItems_SelectionChanged(ByVal WorkItem As Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem) Handles wgt_WorkItems.SelectionChanged
        Try
            If Not SelectedWI Is Nothing Then
                'TODO: End timing and update first
            End If
            SelectedWI = WorkItem
            Me.ux_ServerValue.Text = SelectedWI.Store.TeamFoundationServer.Name
            Me.ux_ProjectValue.Text = SelectedWI.Project.Name
            Me.ux_WIIDValue.Text = SelectedWI.Id
            Me.ux_TitleValue.Text = SelectedWI.Title
            Me.ux_DescriptionValue.Text = SelectedWI.Description
            Me.Show()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub wgt_WorkItems_Started() Handles wgt_WorkItems.Started

    End Sub

#End Region

#Region " Close override "

    Private m_DoExit As Boolean = False

    Private Sub ux_ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_ExitToolStripMenuItem.Click
        m_DoExit = True
        Me.Close()
    End Sub

    Private Sub FormMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If m_DoExit Or e.CloseReason = CloseReason.WindowsShutDown Then
            ExitCalled()
        Else
            e.Cancel = True
            Me.WindowState = FormWindowState.Minimized
        End If
    End Sub

    Private Sub ux_NotifyIcon_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ux_NotifyIcon.MouseDoubleClick
        Me.WindowState = FormWindowState.Normal
        MyBase.SetBounds((Screen.GetWorkingArea(Me).Width - MyBase.Width), (Screen.GetWorkingArea(Me).Height - MyBase.Height), MyBase.Width, MyBase.Height)
    End Sub

#End Region

    ''' <summary>
    ''' Called just before the application exits.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ExitCalled()
        'TODO: Any cleanup needs to be done here
    End Sub



End Class
